import scrapy


class LibgenSpider(scrapy.Spider):
    name = 'libgen'
    allowed_domains = ['libgen.rs']
    start_urls = ['http://libgen.rs/']
    filepath = None
    outfile = None

    def parse(self, response):
        #  获取用户的md5文件
        self.filepath = input('请输入MD5文件所属的绝对路径 : ')
        #  结果保存路径
        self.outfile = input('结果输出路径 : ')
        md5 = None
        #  打开文件
        try:
            with open(self.filepath, 'r', encoding='utf-8') as rf:
                data = rf.readlines()
                #  对数据进行处理
                md5 = [v.strip('| | |-\n') for v in data if '| | |-' in v]
        except FileNotFoundError as nf:
            print(nf, '文件不存在 !')
        # print(md5)
        else:
            for v in md5:
                url = f"http://libgen.rs/search.php?req={v}&lg_topic=libgen&open=0&view=simple&res=25&phrase=1&column=md5"
                yield scrapy.Request(url, callback=self.parse_data, meta={'md5': v, 'outfile': self.outfile})

    #  解析数据
    def parse_data(self, response):
        item = {}
        item['id'] = response.xpath('//table[3]//tr[@bgcolor="#C6DEFF"]//td[1]//text()').extract_first()
        if response.status == 200 and item['id'] is not None:
            #  输出文件路径
            item['outfile'] = response.meta['outfile']
            #  md5值
            item['md5'] = response.meta['md5']
            # 作者
            item['author'] = ','.join(response.xpath('//table[3]//tr[@bgcolor="#C6DEFF"]//td[2]//a/text()').extract())
            #  文章标题
            item['title'] = response.xpath('//table[3]//tr[@bgcolor="#C6DEFF"]//td[3]/a/text()').extract_first()
            #  语言
            item['language'] = ','.join(response.xpath('//table[3]//tr[@bgcolor="#C6DEFF"]//td[7]/text()').extract())
            # print(item)
            yield item
